<template>
  <div id="app">
    <Topbar :key="topbarkey" />
    <Header />
    <router-view />
    <Footer />

    <!-- 登录模块 -->
    <!-- 使用v-if，每一次打开都是重新执行，输入框内容清空 -->
    <Login v-if="isShowLoginModal" />

    <!-- 提示组件 -->
    <transition name="fade">
      <Toast v-show="isShowToast" />
    </transition>
  </div>
</template>

<script>
import Topbar from "./components/Topbar.vue";
import Header from "./components/Header.vue";
import Footer from "./components/Footer.vue";
import Login from "./components/Login.vue";
import { mapState } from "vuex";

import Toast from "./components/Toast.vue";

export default {
  data() {
    return {
      topbarkey: 0,
    };
  },
  components: {
    Topbar,
    Header,
    Footer,
    Login,
    Toast,
  },

  computed: {
    ...mapState({
      isShowLoginModal: (state) => state.showModal.isShowLoginModal,
      isShowToast: (state) => state.showToast.isShowToast,
    }),
  },

  // 方法二： 组件重载
  // 监听路由地址，改变topbar的key值，从实现组件的重载
  watch: {
    "$route.path": {
      handler(newVal, oldVal) {
        this.topbarkey++;           
      },
    },
  },
};
</script>

<style lang="less">
// 开始和结束
.fade-enter,
.fade-leave-to {
  opacity: 0;
}

// 执行过程
.fade-enter-active,
.fade-leave-active {
  transition: opacity 3s linear;
}

// 过渡结束
.fade-enter-to,
.fade-leave {
  opacity: 1;
}
</style>
